package com.dsteiner.gwt.cafeteriasophia.client.controller;

import java.util.List;

import com.dsteiner.gwt.cafeteriasophia.client.ChangeWidget;
import com.dsteiner.gwt.cafeteriasophia.client.ChangeWidget.ChangeModel;
import com.dsteiner.gwt.cafeteriasophia.client.ChangesWidget.ChangesModel;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Change;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Product;
import com.dsteiner.gwt.cafeteriasophia.client.event.AddShiftChangeEvent;
import com.dsteiner.gwt.cafeteriasophia.client.event.ChangesEvent;
import com.dsteiner.gwt.cafeteriasophia.client.event.CheckShiftChangeEvent;
import com.dsteiner.gwt.cafeteriasophia.client.model.ProductModel;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.Widgets.IButton;
import com.dsteiner.widgets.client.Widgets.IFlowPanel;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.controller.PopupOverlayController;
import com.dsteiner.widgets.client.event.EventListener;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Widget;

public class ChangesController {

	private ChangesEvent event;
	private ChangesModel changesModel;
	private PopupOverlayModel popupOverlayModel;
	private IFlowPanel contentPanel;
	private IHTML titleHtml;
	private PrefilledTextBoxModel codeTextBoxModel;

	public void registerChangesWidget(ILazyWidget changesWidget) {
		changesWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
			}
		});
	}

	public void registerChangesModel(ChangesModel changesModel) {
		this.changesModel = changesModel;
	}

	public void registerPopupOverlayModel(PopupOverlayModel popupOverlayModel) {
		this.popupOverlayModel = popupOverlayModel;
	}

	public void registerEventManager(EventManager eventManager) {
		eventManager.registerListener(ChangesEvent.class, new EventListener<ChangesEvent>(){
			public void onEvent(ChangesEvent event) {
				ChangesController.this.event = event;
				List<Change> changes = event.getChanges();
				List<ProductModel> productModels = event.getProductModels();
				titleHtml.setHTML("Cambio de turno con diferencia de Q "+event.getTotalValueChange());
				contentPanel.clear();
				for (Change change : changes) {
					ChangeModel changeModel = new ChangeModel();
					for (ProductModel productModel : productModels) {
						if(productModel.getProduct().equals(change.getProduct())){
							changeModel.setProductModel(productModel);
							break;
						}
					}
					changeModel.setChange(change);
					changeModel.setMoneyChange(event.getChange(Product.dinero));
					ChangeWidget changeWidget = new ChangeWidget(changeModel);
					contentPanel.add((Widget)changeWidget);
				}
				PopupOverlayController.showAtCenter(popupOverlayModel);
			}
		});
		eventManager.registerListener(AddShiftChangeEvent.class, new EventListener<AddShiftChangeEvent>() {
			public void onEvent(AddShiftChangeEvent event) {
				popupOverlayModel.setVisible(false);
			}
		});
	}

	public void registerContentPanel(IFlowPanel contentPanel) {
		this.contentPanel = contentPanel;
	}

	public void registerCheckShiftButton(IButton checkShiftButton) {
		checkShiftButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				popupOverlayModel.setVisible(false);
				EventManagerImpl.getInstance().fireEvent(new CheckShiftChangeEvent());
			}
		});
	}

	public void registerTitleHtml(IHTML titleHtml) {
		this.titleHtml = titleHtml;
	}

	public void registerAddShiftButton(IButton addShiftButton) {
		addShiftButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				double totalValueChange = ChangesController.this.event.getTotalValueChange();
				if(!codeTextBoxModel.isEdited()){
					JsUtil.getInstance().showAlert("ingrese el codigo");
					return;
				}
				try{
					Integer code = new Integer(codeTextBoxModel.getText());
					int correctCode = (int) (totalValueChange / 23);
					if(code == correctCode){
						EventManagerImpl.getInstance().fireEvent(new AddShiftChangeEvent(
								ChangesController.this.event.getShiftChange()));
						popupOverlayModel.setVisible(false);
						return;
					}
				}catch(Exception ex){
				}
				JsUtil.getInstance().showAlert("codigo no correcto");
			}
		});
	}

	public void registerCodeTextBoxModel(PrefilledTextBoxModel codeTextBoxModel) {
		this.codeTextBoxModel = codeTextBoxModel;
	}
}